<template>
  <div style="padding: 0 20px">
    <div class="searchForm_box">
      <el-form :inline="true" :model="searchForm" size="mini" ref="searchForm">
        <el-form-item label="项目名称：" prop="name">
          <el-input v-model="searchForm.name" placeholder="请输入项目名称"></el-input>
        </el-form-item>
        <el-form-item label="行业：" prop="industry_id">
          <el-select v-model="searchForm.industry_id" placeholder="请选择客户行业">
            <el-option
              v-for="item in industry_dict"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="searchForm.daterange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
          <el-button @click="resetForm('searchForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="data_box">
      <el-button @click.native="toCustomerAdd" type="primary" size="mini" v-permission="'customer_add'">报备新客户
      </el-button>
      <!--      <el-badge :value="count" v-if="count != 0" class="item">-->
      <!--        <el-button @click.native="showDialog" type="primary" size="small">申请列表-->
      <!--        </el-button>-->
      <!--      </el-badge>-->
      <!--      <el-button @click.native="showDialog" v-else type="primary" size="small">申请列表-->
      <!--      </el-button>-->
      <el-table size="mini" :data="tableData" border style="width: 100%" v-loading="tableLoading">
        <el-table-column type="index" label="序号" width="50">
          <template slot-scope="scope">
            <span>{{ (listParams.page - 1) * listParams.page_size + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="名称" width="150"></el-table-column>
        <el-table-column prop="address" label="地址" width="150"></el-table-column>
        <el-table-column prop="industry_name" label="行业" width="150"></el-table-column>
        <el-table-column prop="maintenance_staff_name" label="当前维护人员" width="150"></el-table-column>
        <el-table-column label="漏斗信息" min-width="200">
          <template slot-scope="scope">
            {{ getFunnelsName(scope.row.funnels) }}
          </template>
        </el-table-column>
        <el-table-column prop="contract_amount" label="合同总金额(元)" width="150"></el-table-column>
        <el-table-column fixed="right" label="操作" width="400px" align="center">
          <template slot-scope="scope">
            <el-button-group>
              <el-button @click="toCustomerAdd(false,scope.row)" type="primary" size="mini">添加跟进信息</el-button>
              <el-button v-permission="'my_customer_look'" type="primary" size="mini" @click="lookCustomer(scope.row)">
                查看客户详细信息
              </el-button>
              <el-button type="danger" size="mini" @click="relieve(scope.row)">放弃跟进</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="searchForm.page"
      :page-sizes="[10, 20, 50, 100,200,500]"
      :page-size.sync="searchForm.page_size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>


    <!--对话框-->
    <el-dialog title="申请列表" :visible.sync="flowDialogVisible" @close="closeDialog" center width="80%">
      <div class="searchForm_box">
        <el-form :inline="true" :model="flowListParams" ref="flowSearchForm">
          <el-form-item label="客户名称：" prop="name">
            <el-input v-model="flowListParams.name" placeholder="请输入客户名称"></el-input>
          </el-form-item>
          <el-form-item label="行业：" prop="industry_id">
            <el-select v-model="flowListParams.industry_id" placeholder="请选择客户行业">
              <el-option
                v-for="item in industry_dict"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="flowSearch">搜索</el-button>
            <el-button @click="resetForm('flowSearchForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="flowTableData" border v-loading="flowTableLoading">
        <el-table-column type="index" label="序号" width="50">
          <template slot-scope="scope">
            <span>{{ (flowListParams.page - 1) * flowListParams.page_size + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="名称" width="150"></el-table-column>
        <el-table-column label="申请类型" width="150">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.created == 0">跟进报备</el-tag>
            <el-tag v-else type="success">新客户报备</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" min-width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.flow_status == 3">审批中</el-tag>
            <el-tag v-else-if="scope.row.flow_status == 4" type="success">通过</el-tag>
            <el-tag v-else-if="scope.row.flow_status == 2" type="danger">驳回</el-tag>
            <el-tag v-else type="info">未知</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="申请时间" width="200">
          <template slot-scope="scope">
            {{ scope.row.created_at }}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="地址" width="150"></el-table-column>
        <el-table-column prop="industry_name" label="行业" width="150"></el-table-column>
        <el-table-column prop="maintenance_staff_name" label="当前维护人员" width="150"></el-table-column>
        <el-table-column label="漏斗信息" min-width="200">
          <template slot-scope="scope">
            {{ getFunnelsName(scope.row.funnels) }}
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="flowHandleSizeChange"
        @current-change="flowHandleCurrentChange"
        :current-page="flowListParams.page"
        :page-sizes="[10, 20, 50, 100,200,500]"
        :page-size="flowListParams.page_size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="flowTotal"
      >
      </el-pagination>
      <span slot="footer" class="dialog-footer">
        <el-button @click="flowDialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { page as list } from '@/api/customer/customer'
import { getFlowCheckCount, page as flowList } from '@/api/customer/customerFlow'
import { list_all3rd_bycode } from '@/api/business/setting/dict'
import { relieve } from '@/api/customer/customerPool'
import { generateUUID } from '@/utils/util'

export default {
  name: 'MyCustomer',
  data() {
    return {
      searchForm: {
        name: null,
        industry_id: null,
        daterange: null,
        page: 1,
        page_size: 10,
        is_my_customer_page: true
      },
      listParams: {
        name: null,
        industry_id: null,
        page: 1,
        page_size: 10,
        is_my_customer_page: true
      },
      flowListParams: {
        name: null,
        industry_id: null,
        page: 1,
        page_size: 10,
        is_my_customer_page: true
      },
      total: null,
      flowTotal: null,
      tableData: [],//客户信息
      industry_dict: [],//行业字典
      flowTableData: [],//申请信息
      flowDialogVisible: false,
      tableLoading: false,
      flowTableLoading: false,
      count: 0
    }
  },
  created() {
    this.initIndustryDict()
    this.getList()
    this.getFlowCheckCount()
  },
  methods: {
    //flag是否是点击报备新客户按钮
    toCustomerAdd(flag = true, row) {
      const uuid = generateUUID()
      if (flag) {
        this.$store.commit('keep/PUT_PAGE_PARAMS', {
          uuid: uuid,
          data: {}
        })
        this.$router.push({
          name: 'customer_reporting',
          query: {
            uuid: uuid
          }
        })
      } else {
        this.$store.commit('keep/PUT_PAGE_PARAMS', {
          uuid: uuid,
          data: {
            customer_id: row.id
          }
        })
        this.$router.push({
          name: 'customer_reporting',
          query: {
            uuid: uuid
          }
        })
      }
    },
    lookCustomer(row) {
      const uuid = generateUUID()
      this.$store.commit('keep/PUT_PAGE_PARAMS', {
        uuid: uuid,
        data: {
          customer_id: row.id,
          showCreateTAble: true,
          disabled: true
        }
      })
      this.$router.push({
        name: 'my_customer_look_page',
        query: {
          uuid: uuid
        }
      })
    },
    search() {
      this.searchForm.page = 1
      if (this.searchForm.daterange){
        this.searchForm.start = this.searchForm.daterange[0]
        this.searchForm.end = this.searchForm.daterange[1]
      }
      this.listParams = Object.assign({}, this.searchForm)
      this.getList()
    },
    async getFlowCheckCount() {
      await getFlowCheckCount({ is_my_customer_page: true }).then(
        res => {
          this.count = res.data
        }
      )
    },
    flowSearch() {
      this.getFlowList()
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.listParams = Object.assign({}, this.searchForm)
      if (formName == 'searchForm') {
        this.getList()
      } else {
        this.getFlowList()
      }
    },
    handleSizeChange(val) {
      this.listParams.page_size = val
      this.getList()
    },
    flowHandleSizeChange(val) {
      this.flowListParams.page_size = val
      this.getFlowList()
    },
    handleCurrentChange(val) {
      this.listParams.page = val
      this.getList()
    },
    flowHandleCurrentChange(val) {
      this.flowListParams.page = val
      this.getFlowList()
    },
    getList() {
      this.tableLoading = true
      list(this.listParams).then(
        res => {
          this.tableLoading = false
          this.tableData = res.data.records
          this.total = res.data.total
        }
      ).catch(() => {
        this.tableLoading = false
      })
    },
    getFlowList() {
      this.flowTableLoading = true
      flowList(this.flowListParams).then(
        res => {
          this.flowTableLoading = false
          this.flowTableData = res.data.records
          this.flowTotal = res.data.total
        }
      ).catch(() => {
        this.flowTableLoading = false
      })
    },
    //初始化行业字典
    initIndustryDict() {
      list_all3rd_bycode({ code: 'khxg-hy' }).then(
        res => {
          this.industry_dict = res.data.map(item => (
            {
              id: item.id,
              name: item.name
            }
          ))
        }
      )
    },
    //获取漏斗信息
    getFunnelsName(funnels) {
      if (funnels && funnels.length > 0) {
        let str = ''
        for (let i = 0; i < funnels.length; i++) {
          if (i != funnels.length - 1) {
            str += funnels[i].funnel_type_name + ','
          } else {
            str += funnels[i].funnel_type_name
          }
        }
        return str
      } else {
        return ''
      }
    },
    //关闭弹框
    closeDialog() {
      this.flowListParams = {
        name: null,
        industry_id: null,
        page: 1,
        page_size: 10,
        is_my_customer_page: true
      }
    },
    showDialog() {
      this.flowDialogVisible = true
      this.getFlowList()
    },
    relieve(row) {
      this.$confirm('确定解除跟进？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: '提交中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        relieve({ customer_id: row.id, type: 2 }).then(res => {
          this.$message.success('操作成功')
          this.getList()
          loading.close()
        }).catch(() => {
          loading.close()
        })
      }).catch(() => {
      })
    }
  }
}
</script>

<style scoped>
.searchForm_box {
  margin-top: 20px;
  margin-left: 10px;
}

.data_box {
  margin-left: 10px;
}

.el-table {
  margin-top: 10px;
}

.el-pagination {
  margin-top: 20px;
}

.el-badge {
  left: 10px;
  top: -1px;
}
</style>
